package com.homestay.dao.impl;

import com.homestay.beans.Home;
import com.homestay.dao.HomeDao;
import com.homestay.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class HomeDaoImpl implements HomeDao {

    QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());

    @Override
    public int addHome(Home home) {
        try {
            return queryRunner.update("INSERT INTO tb_home  VALUES (null, ?, ?, ?, ?, ?, ?, now(), now())",
                    home.getNum(),
                    home.getH_type(),
                    home.getPrice(),
                    home.getState(),
                    home.getImg(),
                    home.getText()
            );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteHome(int id) {
        try {
            return queryRunner.update("DELETE FROM tb_home WHERE id = ?", id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateHome(Home home) {
        try {
            return queryRunner.update("UPDATE tb_home SET num = ?, h_type = ?, price = ?, state = ?, img = ?, text = ?, updateTime = now() WHERE id = ?",
                    home.getNum(),
                    home.getH_type(),
                    home.getPrice(),
                    home.getState(),
                    home.getImg(),
                    home.getText(),
                    home.getId()
            );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Home getHomeById(int id) {
        try {
            return queryRunner.query("SELECT * FROM tb_home WHERE id = ?",
                new BeanHandler<>(Home.class), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Home> getAllHomes(String num) {
        try {
            if (num == null || num.isEmpty()) {
                return queryRunner.query("SELECT * FROM tb_home order by num", new BeanListHandler<>(Home.class));
            }
            return queryRunner.query("SELECT * FROM tb_home where num=? order by num", new BeanListHandler<>(Home.class),num);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Home> getHomesByState(String state) {
        try {
            if(state == null || state.isEmpty()) {
                return queryRunner.query("SELECT * FROM tb_home", new BeanListHandler<>(Home.class));
            }
            return queryRunner.query("SELECT * FROM tb_home WHERE state = ?",
                    new BeanListHandler<>(Home.class), state);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateHomeState(int id, String state) {
        try {
            return queryRunner.update("UPDATE tb_home SET state = ? WHERE id = ?",
                    state, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
